﻿package mortal.game.view.copy.ZZXYBattle
{
    import com.mui.utils.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.interfaces.*;

    public class ZZXYBattleRewardModule extends BaseWindow
    {
        private var _wave:BitmapNumberText;
        private var _item:BaseItem;
        private var _txtTips:SecTimerView;
        private var _cards:Array;
        private var _rewardDatas:Array;
        private var _giftUid:String;
        private var _closeTimerId:int = -1;

        public function ZZXYBattleRewardModule(param1:ILayer = null)
        {
            super(param1);
            this.setSize(730, 503);
            this.title = Language.getString(20991);
            return;
        }// end function

        public function updateRewardDatas(param1:Array, param2:String, param3:int) : void
        {
            var _loc_5:ZZXYBattleRewardCard = null;
            this._rewardDatas = param1;
            this._giftUid = param2;
            this._wave.text = param3.toString();
            if (param3 < 10)
            {
                this._wave.x = 110;
            }
            else
            {
                this._wave.x = 103;
            }
            var _loc_4:int = 0;
            while (_loc_4 < this._cards.length)
            {
                
                _loc_5 = this._cards[_loc_4];
                _loc_5.reset();
                _loc_4++;
            }
            if (this._txtTips != null)
            {
                this._txtTips.setLeftTime(11);
            }
            return;
        }// end function

        public function updateNum() : void
        {
            var _loc_1:* = GameController.copy.ZZXYBattle.getNum();
            this._item.amount = _loc_1;
            this._item.itemData.extData = _loc_1;
            if (_loc_1 > 0)
            {
                this._item.filters = [];
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_1:int = 0;
            var _loc_3:ZZXYBattleRewardCard = null;
            super.createDisposedChildrenImpl();
            this._cards = [];
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.ZZXYBattleRewardBg, 6, 36, this));
            this._wave = UIFactory.bitmapNumberText(110, 52, "GuideItemUseNum.png", 16, 20, -2, this);
            this._item = UIFactory.baseItem(514, 458, 45, 45, this);
            this._item.isShowLock = true;
            this._item.itemCode = 410021000;
            this._item.amount = 0;
            this._item.filters = [FilterConst.colorFilter];
            _loc_1 = 0;
            while (_loc_1 < 6)
            {
                
                _loc_3 = UICompomentPool.getUICompoment(ZZXYBattleRewardCard) as ZZXYBattleRewardCard;
                _loc_3.openCallback = this.openCardHandler;
                _loc_3.openFinished = this.openEndHandler;
                _loc_3.x = 150 + _loc_1 % 3 * 151;
                _loc_3.y = 110 + int(_loc_1 / 3) * 170;
                _loc_3.index = _loc_1;
                this._cards.push(_loc_3);
                this.addChild(_loc_3);
                _loc_1++;
            }
            var _loc_2:* = GlobalStyle.textFormatPutong;
            _loc_2.size = 14;
            this._txtTips = UIFactory.secTimeView(Language.getString(20992), 570, 478, 200, 24, this, _loc_2);
            this._txtTips.textColor = 16776960;
            this._txtTips.filters = [FilterConst.glowFilter];
            this._txtTips.timeOutHandler = this.timeToAutoGetReward;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (this._giftUid != null)
            {
                GameProxy.sceneProxy.getZZXYBattleCardReward(this._giftUid);
                this._giftUid = null;
            }
            return;
        }// end function

        private function timeToAutoGetReward() : void
        {
            var _loc_1:int = 0;
            var _loc_2:ZZXYBattleRewardCard = null;
            if (isDisposed)
            {
                return;
            }
            this._txtTips.text = Language.getString(20993);
            if (this._cards != null && this._cards.length > 0)
            {
                _loc_1 = int(Math.random() * this._cards.length);
                _loc_2 = this._cards[_loc_1];
                _loc_2.clickMeHandler(null);
            }
            return;
        }// end function

        private function openEndHandler() : void
        {
            var _loc_2:ZZXYBattleRewardCard = null;
            var _loc_1:int = 0;
            while (_loc_1 < this._cards.length)
            {
                
                _loc_2 = this._cards[_loc_1];
                _loc_2.openCard();
                _loc_1++;
            }
            return;
        }// end function

        private function openCardHandler(param1:ZZXYBattleRewardCard) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:ZZXYBattleCardData = null;
            var _loc_6:ZZXYBattleCardData = null;
            var _loc_7:ZZXYBattleRewardCard = null;
            if (this._giftUid != null)
            {
                GameProxy.sceneProxy.getZZXYBattleCardReward(this._giftUid);
                this._giftUid = null;
            }
            if (this._txtTips != null && this._txtTips.running)
            {
                this._txtTips.stop();
                this._txtTips.text = Language.getString(20994);
            }
            if (this._rewardDatas != null)
            {
                _loc_2 = this._rewardDatas.length - 1;
                _loc_3 = 0;
                while (_loc_3 < 50)
                {
                    
                    _loc_4 = int(Math.random() * _loc_2);
                    _loc_5 = this._rewardDatas[0];
                    this._rewardDatas[0] = this._rewardDatas[_loc_4];
                    this._rewardDatas[_loc_4] = _loc_5;
                    _loc_3++;
                }
            }
            _loc_3 = 0;
            while (_loc_3 < this._rewardDatas.length)
            {
                
                _loc_5 = this._rewardDatas[_loc_3];
                if (_loc_5.belongsToMe)
                {
                    _loc_6 = this._rewardDatas[param1.index];
                    this._rewardDatas[param1.index] = _loc_5;
                    this._rewardDatas[_loc_3] = _loc_6;
                    break;
                }
                _loc_3++;
            }
            _loc_3 = 0;
            while (_loc_3 < this._cards.length)
            {
                
                _loc_7 = this._cards[_loc_3];
                _loc_7.updateItem(this._rewardDatas[_loc_3] as ZZXYBattleCardData);
                _loc_7.locked = true;
                _loc_3++;
            }
            this.addCloseTimer();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._item.dispose(param1);
            this._item = null;
            DisplayUtil.delNotUse(this._cards, 0);
            this._cards = null;
            this._rewardDatas = null;
            this._wave.dispose(param1);
            this._wave = null;
            if (this._txtTips != null)
            {
                this._txtTips.stop();
                this._txtTips.dispose();
                this._txtTips = null;
            }
            this.disposeCloseTimer();
            return;
        }// end function

        private function timeToClose() : void
        {
            this.hide();
            return;
        }// end function

        private function addCloseTimer() : void
        {
            this.disposeCloseTimer();
            this._closeTimerId = setTimeout(this.timeToClose, 5000);
            return;
        }// end function

        private function disposeCloseTimer() : void
        {
            if (this._closeTimerId > 0)
            {
                clearTimeout(this._closeTimerId);
                this._closeTimerId = -1;
            }
            return;
        }// end function

    }
}
